/**
 *
 * MTG Life Counter
 * Toolbar container class
 *
 * Created by Nathan Black
 */

#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIBezierPath.h>

#import "LifeCounterToolbar.h"

#define BUTTON_COUNT 3

@interface LifeCounterToolbar (Private)

- (void)newGamePressed;
- (void)historyPressed;
- (void)prevThemePressed;
- (void)nextThemePressed;

@end

@implementation LifeCounterToolbar

- (id)initWithFrame:(struct CGRect)frame;
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        CGRect bounds = [_layer bounds];
        float buttonWidth = bounds.size.width / BUTTON_COUNT;
        NSLog(@"bounds.size.height: %5.1f", bounds.size.height);

        _newGameButton = [[UIPushButton alloc] initWithFrame:CGRectMake(buttonWidth * 0.0f, 0.0f, buttonWidth, bounds.size.height)];
        [_newGameButton setAutosizesToFit:NO];
        [_newGameButton setImage:[UIImage imageNamed:@"newgame2.png"] forState:0]; 
        [_newGameButton addTarget:self action:@selector(newGamePressed) forEvents:3 /* mouse down */];
        [_newGameButton setShowPressFeedback:YES];
        [self addSubview:_newGameButton];

        //_historyButton = [[UIPushButton alloc] initWithFrame:CGRectMake(buttonWidth * 1.0f, 0.0f, buttonWidth, bounds.size.height)];
        //[_historyButton setAutosizesToFit:NO];
        //[_historyButton setImage:[UIImage imageNamed:@"history.png"] forState:0 /*normal state*/];
        //[_historyButton addTarget:self action:@selector(historyPressed) forEvents:2 /* mouse down */];
        //[_historyButton setShowPressFeedback:YES];
        //[self addSubview:_historyButton];

        _prevThemeButton = [[UIPushButton alloc] initWithFrame:CGRectMake(buttonWidth * 1.0f, 0.0f, buttonWidth, bounds.size.height)];
        [_prevThemeButton setAutosizesToFit:NO];
        [_prevThemeButton setImage:[UIImage imageNamed:@"leftarrow.png"] forState:0 /*normal state*/];
        [_prevThemeButton addTarget:self action:@selector(prevThemePressed) forEvents:3 /* mouse down */];
        [_prevThemeButton setShowPressFeedback:YES];
        [self addSubview:_prevThemeButton];

        _nextThemeButton = [[UIPushButton alloc] initWithFrame:CGRectMake(buttonWidth * 2.0f, 0.0f, buttonWidth, bounds.size.height)];
        [_nextThemeButton setAutosizesToFit:NO];
        [_nextThemeButton setImage:[UIImage imageNamed:@"rightarrow.png"] forState:0 /*normal state*/];
        [_nextThemeButton addTarget:self action:@selector(nextThemePressed) forEvents:3 /* mouse down */];
        [_nextThemeButton setShowPressFeedback:YES];
        [self addSubview:_nextThemeButton];

        CFRelease(colorSpace);
    }
    return self;
}

- (void)dealloc
{
    [_newGameButton release];
    _newGameButton = nil;
    [_historyButton release];
    _historyButton = nil;

    [super dealloc];
}

- (void)setDelegate:(id)object
{
    _delegate = object;
}

- (void)newGamePressed
{
    NSLog(@"Toolbar newGamePressed");
    if (_delegate && [_delegate respondsToSelector:@selector(newGamePressed)]) {
        [_delegate newGamePressed];
    }
}

- (void)historyPressed
{
    if (_delegate && [_delegate respondsToSelector:@selector(historyPressed)]) {
        [_delegate historyPressed];
    }
}

- (void)prevThemePressed
{
    NSLog(@"Toolbar prevThemePressed");
    if (_delegate && [_delegate respondsToSelector:@selector(prevThemePressed)]) {
        [_delegate prevThemePressed];
    }
}

- (void)nextThemePressed
{
    NSLog(@"Toolbar nextThemePressed");
    if (_delegate && [_delegate respondsToSelector:@selector(nextThemePressed)]) {
        [_delegate nextThemePressed];
    }
}

- (BOOL)isOpaque
{
    return NO;
}

- (void)drawRect:(struct CGRect)rect
{
    UIBezierPath *path = [UIBezierPath roundedRectBezierPath:rect withRoundedCorners:10 withCornerRadius:18.0];
    const float backgroundComponents[4] = {0, 0, 0, 0.7};
    CGContextSetFillColor(UICurrentContext(), backgroundComponents);
    [path fill];

    [super drawRect:rect];
}

@end
